// This is part of the iostream library, providing input/output for C++.
// Copyright (C) 1992 Per Bothner.
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
extern "C" _G_pid_t vfork(void);
extern "C" int vfork(void);
procbuf::procbuf(const char *command, int mode) : filebuf()
procbuf *procbuf::open(const char *command, int mode)
int parent_end, child_end;
if (::pipe(pipe_fds) < 0)
parent_end = pipe_fds[0];
read_or_write = _S_NO_WRITES;
parent_end = pipe_fds[1];
read_or_write = _S_NO_READS;
int child_std_end = mode == ios::in ? 1 : 0;
if (child_end != child_std_end) {
::dup2(child_end, child_std_end);
::execl("/bin/sh", "sh", "-c", command, NULL);
_fb._fileno = parent_end;
xsetflags(read_or_write, _S_NO_READS|_S_NO_WRITES);
/* #define USE_SIGMASK */
int status = filebuf::sys_close();
#if defined(SIG_BLOCK) && defined(SIG_SETMASK)
sigaddset (&set, SIGINT);
sigaddset (&set, SIGQUIT);
sigaddset (&set, SIGHUP);
sigprocmask (SIG_BLOCK, &set, &oset);
int mask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGHUP));
typedef void (*void_func)(int);
void_func intsave = (void_func)signal(SIGINT, SIG_IGN);
void_func quitsave = (void_func)signal(SIGQUIT, SIG_IGN);
void_func hupsave = (void_func)signal(SIGHUP, SIG_IGN);
while ((wait_pid = wait(&wstatus)) != _pid && wait_pid != -1) { }
#if defined(SIG_BLOCK) && defined(SIG_SETMASK)
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
signal(SIGQUIT, quitsave);